package java44th.group.service.wx.topic;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java44th.group.bean.common.BasePageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketGoods;
import java44th.group.bean.po.MarketTopic;
import java44th.group.bean.po.MarketTopicExample;
import java44th.group.bean.wx.vo.topic.DetailGoodsVo;
import java44th.group.bean.wx.vo.topic.DetailVo;
import java44th.group.bean.wx.vo.topic.ListVo;
import java44th.group.bean.wx.vo.topic.RelatedTopicVo;
import java44th.group.mapper.MarketGoodsMapper;
import java44th.group.mapper.MarketTopicMapper;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName project-second
 * @Descriptiom TODO
 * @Aurhor: 蔡文强
 * @Since: 2022/10/15 10:36
 */
@Service
public class WxTopicServiceImpl implements WxTopicService {

    @Autowired
    MarketTopicMapper marketTopicMapper;


    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public CommonData<ListVo> queryTopicList(BasePageInfo basePageInfo) {

        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketTopicExample example = new MarketTopicExample();
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(example);

        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopics);

        ArrayList<ListVo> listVos = new ArrayList<>();
        for (MarketTopic marketTopic : marketTopics) {
            ListVo listVo = new ListVo(marketTopic.getPicUrl(), marketTopic.getPrice().doubleValue(),
                    marketTopic.getSubtitle(), marketTopic.getId(), marketTopic.getTitle(),
                    marketTopic.getReadCount());

            listVos.add(listVo);
        }

        return CommonData.dataWithList(marketTopicPageInfo, listVos);
    }

    @Override
    public DetailVo queryTopicInfoById(Integer id) {

        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(example);
        MarketTopic marketTopic = marketTopics.get(0);
        // 得到相关的商品id列表
        Integer[] goods = marketTopic.getGoods();

        ArrayList<DetailGoodsVo> detailGoodsVos = new ArrayList<>();
        for (Integer good : goods) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(good);
            DetailGoodsVo detailGoodsVo = new DetailGoodsVo(marketGoods.getBrief(),
                    marketGoods.getCounterPrice(),marketGoods.getId(),
                    marketGoods.getIsHot(),marketGoods.getIsNew(),
                    marketGoods.getName(),marketGoods.getPicUrl(),
                    marketGoods.getRetailPrice());

            detailGoodsVos.add(detailGoodsVo);
        }


        DetailVo detailVo = new DetailVo();
        detailVo.setTopic(marketTopic);
        detailVo.setGoods(detailGoodsVos);

        return detailVo;
    }


    // 优化点  TODO
    // 相同的商品越多，优先放置该商品
    // 且该商品放在第一位
    @Override
    public CommonData<MarketTopic> queryRelatedTopic(Integer id) {

        MarketTopicExample example1 = new MarketTopicExample();
        MarketTopicExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andIdEqualTo(id);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(example1);
        MarketTopic marketTopic = marketTopics.get(0);
        Integer[] goods = marketTopic.getGoods();

        // 创建要返回的相关专题的列表
        ArrayList<MarketTopic> relatedTopics = new ArrayList<>();


        PageHelper.startPage(1, 4);
        // 从所有商品中找到相关的商品
        MarketTopicExample example2 = new MarketTopicExample();
        List<MarketTopic> allTopics = marketTopicMapper.selectByExample(example2);

        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(allTopics);


        return CommonData.data(marketTopicPageInfo);
    }


    // 实际只要返回一个list就行
    @Override
    public RelatedTopicVo queryRelatedTopic2(Integer id) {
        MarketTopicExample example1 = new MarketTopicExample();
        MarketTopicExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andIdEqualTo(id);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(example1);
        MarketTopic marketTopic = marketTopics.get(0);
        Integer[] goods = marketTopic.getGoods();

        // 创建要返回的相关专题的列表
        ArrayList<MarketTopic> relatedTopics = new ArrayList<>();

        // 从所有商品中找到相关的商品
        MarketTopicExample example2 = new MarketTopicExample();
        List<MarketTopic> allTopics = marketTopicMapper.selectByExample(example2);


        // 瞎写的限制专题出现数量
        while (allTopics.size() > 4) {
            allTopics.remove(0);
        }

        RelatedTopicVo<MarketTopic> relatedTopicVo = new RelatedTopicVo<>(allTopics);
        return relatedTopicVo;
    }


}
